package <render>packageName</render>;

<block v-repeat="item in imports">import <render>item</render>;
</block>

public class <render>className</render> extends <render>name</render>TypeSelf implements Bo {

    private <render>name</render>Repository repository;

    <render>name</render>(<render>name</render>Repository repository) {
        this.repository = repository;
    }

    public boolean query(OuterExpression expression) {
        return repository.querySelf(this, expression);
    }

    public boolean query(InnerCompare expression) {
        return repository.querySelf(this, expression);
    }

    synchronized
    public boolean query() {
        if (this._id == null) {
            return false;
        }
        try {
            return this.repository.queryById(this);
        } catch (Exception e) {
            handler.handle(e);
            return false;
        }
    }

    synchronized
    public void reset() {
        if (this.vo != null) {
            this.vo.reset();
        }
    }

    private ExceptionHandler handler=ExceptionHandler.print;

    public void handleException(ExceptionHandler handler) {
        this.handler = handler;
    }

    <render>name</render>Vo vo;

    @Override
    public <render>name</render>Vo getVo() {
        return vo;
    }

    void createVo() {
        vo = new <render>name</render>Vo();
        vo.setBe(new <render>name</render>Be());
    }

    boolean queryByHolder(Bo holder,String selfName) {
        return this.repository.queryByHolder(holder, this, selfName);
    }

    synchronized
    public void save() {
        this.repository.save(this);
        if (this.vo != null) {
            this.vo.setBe(this.vo.getBe());
        }
    }

    synchronized
    public void delete() {
        this.repository.delete(this);
    }

    private String _id;
    synchronized
    public String getId() {
        return this._id;
    }

    synchronized
    public void setId(String id) {
        this._id = id;
    }

    <block v-repeat="field in fields">
    synchronized
    public <render>field.typeClass</render> get<render>field.hName</render>() {
        if (vo == null) {
            return null;
        }
        return vo.get<render>field.hName</render>();
    }

    synchronized
    public void set<render>field.hName</render>(<render>field.typeClass</render> v) {
        if (vo == null) {
            createVo();
        }
        vo.set<render>field.hName</render>(v);
    }
    </block>

    <block v-repeat="quote in quotes">
    <render>quote.type.name</render> _quote_<render>quote.name</render>;
    synchronized
    public <render>quote.type.name</render> get<render>quote.hName</render>() {
        if (this._quote_<render>quote.name</render> == null) {
            synchronized (this) {
                if (this._quote_<render>quote.name</render> == null) {
                    try {
                        this.repository.query<render>quote.hName</render>(this);
                    } catch (Exception e) {
                        handler.handle(e);
                    }
                }
            }
        }
        return _quote_<render>quote.name</render>;
    }

    public boolean attach<render>quote.hName</render>(Bo bo) {
        boolean r= repository.attach(this, bo, "<render>quote.name</render>");
        if (r) {
            this._quote_<render>quote.name</render> = (<render>quote.type.name</render>)bo;
        }
        return r;
    }

    public void detach<render>quote.hName</render>() {
        if (this._quote_<render>quote.name</render> == null) {
            get<render>quote.hName</render>();
        }
        repository.detach(this, this._quote_<render>quote.name</render>, "<render>quote.name</render>");
    }

    </block>

}

